package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.CommunityDesActivity;
import com.winz.cultureapp.adapter.xrcv.CollectionCommunityAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.UserCollectionBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的收藏--文化社团
 */
public class CollectionItem3Fragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_data)
    public XRecyclerView mXrcv_list;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;
    
    private List<UserCollectionBean.DataBean.ListBean> mDatas;
    private CollectionCommunityAdapter mAdapter;



    public static CollectionItem3Fragment getInstance(){
        CollectionItem3Fragment fragment = new CollectionItem3Fragment();

        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_colletion_item3;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_list.setLayoutManager(manager);
        mXrcv_list.setPullRefreshEnabled(true);
        mXrcv_list.setLoadingMoreEnabled(true);
        mAdapter = new CollectionCommunityAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_community_list);
    }

    @Override
    protected void initEvents() {

        mXrcv_list.setAdapter(mAdapter);
        mXrcv_list.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void loadDatas() {
        getList();
    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGetByToken("user_collection", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), RequestFactory.getInstance().user_collection(page + "",limit + "","2"),new OnResponeListener<UserCollectionBean>() {
            @Override
            public void onSuccess(UserCollectionBean UserCollectionBean) {
                Logger.d("user_collection==>>", JsonUtil.getJsonFromObj(UserCollectionBean));
                if (UserCollectionBean.code == 0) {
//                            totalPage = bean.data.totalPage;
                    if (UserCollectionBean.data.list != null && UserCollectionBean.data.list.size() > 0) {
                        mXrcv_list.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = UserCollectionBean.data.list;
                        } else {
                            mDatas.addAll(UserCollectionBean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        UserCollectionBean.DataBean.ListBean empty = new UserCollectionBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_list.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(UserCollectionBean.msg);
                }
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("user_collection==>>", error);
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_list.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.COMMUNITY.ITEM_ID,mDatas.get(position).leagueId);
        bundle.putString(Constants.KEY.COMMUNITY.ITEM_DES_HREF,Constants.URL.IP + "/league/detail/" + mDatas.get(position).leagueId);

        startActivity(CommunityDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
